<template>
  <div>
    <pannel title="文件上传案例">
      <el-upload
        class="avatar-uploader"
        :action="acitonUrl"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </pannel>
  </div>
</template>

<script>
import Pannel from "@/components/Pannel.vue";
export default {
  data() {
    return {
        acitonUrl:'',
        imageUrl:'',
        baseUrl:'http://127.0.0.1:5000'
    };
  },
  components: {
    Pannel,
  },
  methods:{
      handleAvatarSuccess(res){
          this.imageUrl = this.baseUrl+res.imgUrl
          console.log(res);
          this.$bus.$emit('changeUserInfo','')
      },
      beforeAvatarUpload(file){
          console.log(file);
      }

  },
  created() {
      let id = JSON.parse(localStorage.getItem('user')).id
    this.acitonUrl = this.baseUrl+'/users/avatar_upload?id='+id;
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>